fix: handle VS Code LM API changes for token counting #6291
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes issue #6290 where the VS Code Language Model API was unable to correctly calculate context size when using models like "copilot - gpt-4.1". The issue was caused by VS Code updating their
countTokensAPI to expectLanguageModelChatMessage2instead ofLanguageModelChatMessage.Problem
countTokensmethod now expects a different message formatSolution
Updated
internalCountTokensmethod to handle both old and new API versions:LanguageModelChatMessageinstances and string inputsEnhanced
calculateTotalInputTokensmethod:Added comprehensive test coverage:
Testing
Impact
This fix ensures that Roo Code continues to work correctly with VS Code's Language Model API regardless of which version of the API is being used, providing a seamless experience for users.
Fixes #6290
Important
Fixes VS Code LM API token counting by updating methods to handle new API changes and adding a tiktoken fallback.
internalCountTokensinvscode-lm.tsto handle bothLanguageModelChatMessageandLanguageModelChatMessage2.calculateTotalInputTokensto attempt batch token counting first.vscode-lm.spec.tsfor fallback behavior when VS Code API returns 0, throws an error, or returns negative values.fallbackToTiktokenmethod for token counting fallback.This description was created by
for 6722ae2. You can customize this summary. It will automatically update as commits are pushed.